package org.blogwu.learn.multithread.chapter2.demo2;

/**
 * 当线程在sleep时,进行中断,会抛出异常
 *
 * @author hyf
 * @date 2018-09-11
 **/
public class Main2 {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("Interrupted");
                    break;
                }

                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    System.out.println("InterruptedException");
                    // 让中断继续进行
                    Thread.currentThread().interrupt();
                }
            }
            System.out.println("exit");
        });
        t1.start();
        Thread.sleep(2000);
        t1.interrupt();
        Thread.sleep(3000);
        System.out.println(t1.getState());
    }
}
